package com.swt;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.junit.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

import junit.framework.Assert;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection; 

@RunWith(Parameterized.class)
public class Tesss {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  String m_height;
  String m_width;
  String m_bmi;
  @Parameters    
  public static Collection data(){
          return Arrays.asList(new Object[][]{
                  {"160","30","17"},
                  {"180","60","14"},
                  {"150","60","20"},
             });
  }
//构造函数，对变量进行初始化
  public Tesss(String h, String w,String i){
        this.m_height = h;
        this.m_width = w;
        this.m_bmi = i;
  }
  
  @Before
  public void setUp() throws Exception {
	System.setProperty("webdriver.firefox.bin", "D:\\fireFox\\firefox.exe");
	System.setProperty("webdriver.gecko.driver", "D:\\fireFox\\resource\\geckodriver.exe");
	driver = new FirefoxDriver();
    baseUrl = "http://cn.onlinebmicalculator.com/";
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	driver.get(baseUrl + "/");
  }
	
  @Test
  public void testTesss() throws Exception {
    driver.findElement(By.name("f_height")).clear();
    driver.findElement(By.name("f_height")).sendKeys(m_height);
    driver.findElement(By.name("f_weight")).clear();
    driver.findElement(By.name("f_weight")).sendKeys(m_width);
    driver.findElement(By.name("f_submit")).click();
    //WebElement element=driver.findElement(By.className("bmi_info"));
    WebElement element=driver.findElement(By.className("bmi_info")).findElement(By.tagName("span"));
    String bmi =element.getText();
    System.out.print(bmi);
    Assert.assertEquals(m_bmi, bmi);
  }

  
  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
  
  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
